/** Lachlan Hewson-Doherty (h201384171)
 *  Diploma of Digital and Interactive Games
 *  lachlan_doherty@live.com.au
 *  2013
 */

package cabbage.model.logic;

import java.sql.SQLException;

import cabbage.controller.Cupcake;
import cabbage.utilities.CabbageUtility;



public class AccountCreation {
	

	
	
	

	
	// ---------------------------- CONSTANTS ADJUSTABLE HERE ---------------------------- \\
		
		private final int MIN_USERNAME = 6;
		private final int MAX_USERNAME = 20;
		
		private final int MIN_FIRSTNAME = 3;
		private final int MAX_FIRSTNAME = 50;
		
		private final int MIN_LASTNAME = 3;
		private final int MAX_LASTNAME = 50;
		
		private final int MIN_EMAIL = 5;
		private final int MAX_EMAIL = 150;
		
		private final int MIN_PASS = 6;
		private final int MAX_PASS = 24;

	// ---------------------------- CONSTANTS ADJUSTABLE HERE ---------------------------- \\
		
		
		
		private Cupcake cc;
		
		
		
		public AccountCreation(){
			
			
		}
		
		
		
		
		
		public boolean createAccount (String username, String firstname, String lastname, String emailaddress, String password) throws SQLException {
			
			if (validateUserName(username)
					
					&& validateFirstName
									(firstname)
					&& validateLastName
									(lastname)
					&& validateEmailAddress
									(emailaddress)
					&& validatePassword
									(password)) 
				
			{	//If TRUE - insert new account here...
				
				cc = new Cupcake();
				
				int newAccountID = cc.insertNewAccount(username, firstname, lastname, emailaddress, password);
				
				if(newAccountID > 0) {
				//If successful insert a new player...
					cc.insertNewPlayer(newAccountID);
				}
				
				else
				{
				return false;
				}
				
				return true;
			}
			return false;
		}
		
		
		
		
		
		
		public boolean validateUserName (String username) {
			
			if (CabbageUtility.checkLengthOfString(username, MIN_USERNAME, MAX_USERNAME))
											return true;
			return false;
		}
		
		
		
		
		
		
		public boolean validateFirstName (String firstname) {
			
			if (CabbageUtility.checkLengthOfString(firstname, MIN_FIRSTNAME, MAX_FIRSTNAME))
											return true;
			return false;
		}
		
		
		
		
		
		
		public boolean validateLastName (String lastname) {
			
			if (CabbageUtility.checkLengthOfString(lastname, MIN_LASTNAME, MAX_LASTNAME))
											return true;
			return false;
		}
		
		
		
		
		
		
		public boolean validateEmailAddress (String emailaddress) throws SQLException {
			
			if (! CabbageUtility.checkLengthOfString(emailaddress, MIN_EMAIL, MAX_EMAIL)) return false;
			{
				if (cc.checkEmailAddressExists(emailaddress)) return false;
				
			}
			return true;
		}
		
		
		
		
		
		public static boolean isEmailFormat(String string) {
	        if (! string.matches 
	        		("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$")) return false;
	        
	        return true;
	}
		
		
		
		

		
		
		public boolean validatePassword (String password) {
			
			if (CabbageUtility.checkLengthOfString(password, MIN_PASS, MAX_PASS))
											return true;
			return false;
		}

}
